﻿using Ftcb.Common;
using Kanban.DALContainer;
using Kanban.IBLL;
using Kanban.IDal;
using Kanban.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kanban.BLL
{
    public class BoardService : BaseService<Board>, IBoardService
    {

        private readonly IBoardDal BoardDal = Container.Resolve<IBoardDal>();
        private readonly ISpeed_SettingDal Speed_SettingDal = Container.Resolve<ISpeed_SettingDal>();

        public override void SetCurrentDal()
        {
            Dal = BoardDal;
        }
        public Task<object> GetNum()
        {
            return BoardDal.GetNum();
        }
        public Task<object> GetSystemInfo()
        {
            return BoardDal.GetSystemInfo();
        }

        public async Task<Tuple<object, List<Speed_Setting>>> GetSystemInfos()
        {
            object info = await GetSystemInfo();
            List<Speed_Setting> list = await GetCache_SpeedSetting();
            return new Tuple<object, List<Speed_Setting>>(info, list);
        }

        public async Task<List<Speed_Setting>> GetCache_SpeedSetting()
        {
            List<Speed_Setting> list;
            string key = "GetCache_SpeedSetting";
            if (CacheHelper.GetCache(key) != null)
            {
                list = CacheHelper.GetCache<List<Speed_Setting>>(key);
            }
            else
            {
                list = (await Speed_SettingDal.GetListAsync("1=1")).ToList();
                CacheHelper.SetCache(key, list);
            }
            return list;
        }

        public Task<IEnumerable<object>> GetRacks()
        {
            return BoardDal.GetRacks();
        }

        public Task<Tuple<IList<object>, int>> GetInPageData(string PKGID, string Location, string StoreCode, string SHELFTNO, string PN, string startTime, string endTime, int pageSize, int pageIndex, bool Qty = false)
        {
            return BoardDal.GetInPageData(PKGID, Location,  StoreCode,  SHELFTNO, PN, startTime, endTime, pageSize, pageIndex, Qty);
        }

        public Task<Tuple<IList<object>, int>> GetOutPageData(string TaskNo, string PKGID, string Location, string StoreCode, string SHELFTNO, string PN, string startTime, string endTime, int pageSize, int pageIndex)
        {
            return BoardDal.GetOutPageData(TaskNo, PKGID, Location,  StoreCode,  SHELFTNO, PN, startTime, endTime, pageSize, pageIndex);
        }

        public Task<Tuple<IList<object>, int>> GetTaskPageData(string TaskNo, string PKGID, string Location, string StoreCode, string SHELFTNO, string PN, int pageSize, int pageIndex)
        {
            return BoardDal.GetTaskPageData(TaskNo, PKGID, Location,  StoreCode,  SHELFTNO, PN, pageSize, pageIndex);
        }

        public Task<Tuple<IList<object>, int>> GetTaskCancelPageData(string search, int pageSize, int pageIndex)
        {
            return BoardDal.GetTaskCancelPageData(search, pageSize, pageIndex);
        }

        public Task<Tuple<IList<object>, int>> GetWarehousePageData(string PKGID, string Location, string StoreCode, string SHELFTNO, string PN, int pageSize, int pageIndex)
        {
            return BoardDal.GetWarehousePageData(PKGID, Location,  StoreCode,  SHELFTNO, PN, pageSize, pageIndex);
        }

        public Task<IEnumerable<object>> GetSysStatus()
        {
            return BoardDal.GetSysStatus();
        }

        public Task<bool> UpdateRunStatus(bool status)
        {
            return BoardDal.UpdateRunStatus(status);
        }

        public Task<Tuple<bool, string>> DelTask(string taskNo)
        {
            return BoardDal.DelTask(taskNo);
        }

        public Task<Tuple<bool, string>> DelWarehouse(string serialNo)
        {
            return BoardDal.DelWarehouse(serialNo);
        }

        public Task<Tuple<IList<object>, int>> GetApiLogPageData(string time, int pageSize, int pageIndex)
        {
            return BoardDal.GetApiLogPageData(time, pageSize, pageIndex);
        }

        public Task<Tuple<IList<object>, int>> GetLogPageData(string time, int pageSize, int pageIndex)
        {
            return BoardDal.GetLogPageData(time, pageSize, pageIndex);
        }

        public async Task<bool> BatchCompleteTask(string ids)
        {
            var list = (await BoardDal.GetTaskList(ids)).ToList();
            list.ForEach(item =>
            {
                BoardDal.CompleteTask(item.TASKNO, item.PKGID);
            });
            return true;
        }

        public Task<IEnumerable<object>> GetWarehouseWarnList()
        {
            return BoardDal.GetWarehouseWarnList();
        }

        public async Task<Tuple<object, List<TotalModel>, List<TotalModel>, IEnumerable<object>, IEnumerable<object>>> GetBoardData()
        {
            var num = await BoardDal.GetNum();
            var inData = await GetInWarehouse4WMSTotal();
            var outData = await GetAPI_TASK_FINISHTotal();
            var warnData = await BoardDal.GetWarehouseWarnList();
            var task = await BoardDal.GetAllTask();
            return new Tuple<object, List<TotalModel>, List<TotalModel>, IEnumerable<object>, IEnumerable<object>>(num, inData, outData, warnData, task);
        }

        public async Task<List<TotalModel>> GetInWarehouse4WMSTotal()
        {
            var inData = await BoardDal.GetInWarehouse4WMSTotal();
            return GetTotalModelData(inData);
        }

        public async Task<List<TotalModel>> GetAPI_TASK_FINISHTotal()
        {
            var inData = await BoardDal.GetAPI_TASK_FINISHTotal();
            return GetTotalModelData(inData);
        }

        public List<TotalModel> GetTotalModelData(IEnumerable<TotalModel> inData)
        {
            List<TotalModel> data = new List<TotalModel>();
            int hour = DateTime.Now.Hour;
            if (hour % 2 == 1)
            {
                hour--;
            }
            for (int i = 0; i < 6; i++)
            {
                if (hour < 0)
                {
                    hour = 24;
                }
                TotalModel model = new TotalModel();
                model.Time = $"{hour}-{hour + 2}";
                model.Count = inData.Where(p => p.Hour == hour || p.Hour == hour + 1).Sum(p => p.Count);
                data.Insert(0, model);
                hour -= 2;
            }
            return data;
        }
    }
}
